//Tag: BFS
#include <algorithm>
#include <cstdio>
#include <queue>

const int N = 2e3 + 5, inf = 0x3f3f3f3f, dx[8] = {1, 1, 1, 0, 0, -1, -1, -1},
          dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};

int n, m, sx, sy, dis[N][N];
int lx = -1, ly = -1;
char map[N][N];

struct Node {
  int x, y;
  Node(int x, int y) : x(x), y(y){};
};
std::queue<Node> q;

inline void solve() {
  while (!q.empty()) {
    Node u = q.front();
    q.pop();
    for (int i = 0, tx, ty; i < 8; ++i) {
      tx = u.x + dx[i], ty = u.y + dy[i];
      if (dis[tx][ty] != -1 && dis[tx][ty] > dis[u.x][u.y] + 1) {
        dis[tx][ty] = dis[u.x][u.y] + 1;
        q.push(Node(tx, ty));
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("forest.in", "r", stdin);
  freopen("forest.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  for (int i = 0; i <= n + 1; ++i) dis[i][0] = dis[i][m + 1] = -1;
  for (int i = 0; i <= m + 1; ++i) dis[0][i] = dis[n + 1][i] = -1;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", map[i] + 1);
    for (int j = 1; j <= m; ++j) {
      if (map[i][j] == '*') sx = i, sy = j;
      if (map[i][j] == 'X') {
        dis[i][j] = -1;
        if (lx == -1 || ly <= j) lx = i, ly = j;
      }
    }
  }
  for (int i = ++ly + 1; i <= m; ++i) dis[lx][i] = -1;
  if (dis[sx][sy] == -1) {
    for (int i = ly + 1; i <= m; ++i) dis[lx][i] = 0;
    lx = ly = -1;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        if (map[i][j] == 'X') {
          dis[i][j] = -1;
          if (lx == -1 || ly >= j) lx = i, ly = j;
        }
      }
    }
    for (int i = --ly - 1; i; --i) dis[lx][i] = -1;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (dis[i][j] != -1) dis[i][j] = inf;
  dis[lx][ly] = 0;
  int ans = 0;
  for (int i = 0, tx, ty; i < 3; ++i) {
    tx = lx + dx[i], ty = ly + dy[i];
    if (dis[tx][ty] != -1) {
      dis[tx][ty] = 1;
      q.push(Node(tx, ty));
    }
  }
  solve();
  ans += dis[sx][sy];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (dis[i][j] != -1) dis[i][j] = inf;
  dis[lx][ly] = 0;
  for (int i = 5, tx, ty; i < 8; ++i) {
    tx = lx + dx[i], ty = ly + dy[i];
    if (dis[tx][ty] != -1) {
      dis[tx][ty] = 1;
      q.push(Node(tx, ty));
    }
  }
  solve();
  ans += dis[sx][sy];
  printf("%d", ans);
  return 0;
}